"""
商户进件相关模型类
"""

from jlpay.model.base import OrgBaseReq, BaseResponse
from jlpay.utils.annotation_utils import Encrypt, Sensitive, SensitiveField


@Encrypt  # 标记整个类需要加密处理
class MerchantIncomingAddRequest(OrgBaseReq):
    """商户进件请求"""
    
    API_URL = "/open/merch/access/incoming/add"
    
    def __init__(self):
        super().__init__()
        self.base_info = None  # 商户基础信息
        self.identification_info = None  # 商户身份信息
        self.business_info = None  # 商户经营信息
        self.settlement_info = None  # 商户结算账户信息
        self.settlement_rule = None  # 商户结算规则、费率规则
        self.additional_info = None  # 渠道信息、银行合作信息等
        self.sms_config = None  # 是否给商户发短信
        self.func_info = None  # 开通商户业务功能
        self.chain_store_info = None  # 品牌连锁商户进件配置信息
        self.brand_info = None  # 品牌信息
        
        # 定义敏感字段路径
        self._sensitive_fields = [
            # 基础信息中的敏感字段（根据ContactInfoDto.java的@Encrypt注解）
            SensitiveField("base_info.contact_info.contact_name"),
            SensitiveField("base_info.contact_info.contact_phone"),
            SensitiveField("base_info.contact_info.contact_id_no"),
            
            # 身份信息中的敏感字段（根据IdentityInfoDto.java的@Encrypt注解）
            SensitiveField("identification_info.identity_info.id_card_name"),
            SensitiveField("identification_info.identity_info.id_card_english_name"),
            SensitiveField("identification_info.identity_info.id_card_no"),
            SensitiveField("identification_info.identity_info.legal_person_phone"),
            SensitiveField("identification_info.identity_info.id_card_address"),
            
            # 结算信息中的敏感字段（根据AccountInfoDto.java的@Encrypt注解）
            SensitiveField("settlement_info.account_info.account_no"),
            SensitiveField("settlement_info.account_info.account_name"),
            SensitiveField("settlement_info.account_info.bank_set_phone"),
            SensitiveField("settlement_info.account_info.delegatee_name"),
            SensitiveField("settlement_info.account_info.delegatee_id_card"),
            SensitiveField("settlement_info.account_info.delegatee_phone"),
            
            # 补充信息中的敏感字段（根据SpecsInfoDto.java的@Encrypt注解）
            SensitiveField("additional_info.specs_info.account_no"),
            SensitiveField("additional_info.specs_info.account_name"),

            # 品牌信息中的敏感字段（根据BrandDto.java的@Encrypt注解）
            SensitiveField("brand_info.contact_name"),
            SensitiveField("brand_info.contact_phone"),
        ]
    
    def get_sensitive_fields(self):
        """
        返回需要加密的敏感字段路径列表
        
        Returns:
            包含敏感字段路径的列表
        """
        return self._sensitive_fields
    
    def set_base_info(self, base_info):
        """
        设置商户基础信息
        
        Args:
            base_info: 基础信息字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.base_info = base_info
        return self
    
    def set_identification_info(self, identification_info):
        """
        设置商户身份信息
        
        Args:
            identification_info: 身份信息字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.identification_info = identification_info
        return self
    
    def set_business_info(self, business_info):
        """
        设置商户经营信息
        
        Args:
            business_info: 经营信息字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.business_info = business_info
        return self
    
    def set_settlement_info(self, settlement_info):
        """
        设置商户结算账户信息
        
        Args:
            settlement_info: 结算账户信息字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.settlement_info = settlement_info
        return self
    
    def set_settlement_rule(self, settlement_rule):
        """
        设置商户结算规则、费率规则
        
        Args:
            settlement_rule: 结算规则字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.settlement_rule = settlement_rule
        return self
    
    def set_additional_info(self, additional_info):
        """
        设置渠道信息、银行合作信息等
        
        Args:
            additional_info: 附加信息字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.additional_info = additional_info
        return self
    
    def set_sms_config(self, sms_config):
        """
        设置是否给商户发短信
        
        Args:
            sms_config: 短信配置字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.sms_config = sms_config
        return self
    
    def set_func_info(self, func_info):
        """
        设置开通商户业务功能
        
        Args:
            func_info: 业务功能字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.func_info = func_info
        return self
    
    def set_chain_store_info(self, chain_store_info):
        """
        设置品牌连锁商户进件配置信息
        
        Args:
            chain_store_info: 品牌连锁信息字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.chain_store_info = chain_store_info
        return self
    
    def set_brand_info(self, brand_info):
        """
        设置品牌信息
        
        Args:
            brand_info: 品牌信息字典
            
        Returns:
            MerchantIncomingAddRequest实例
        """
        self.brand_info = brand_info
        return self
    
    def check_required_params(self, config):
        """
        检查必需参数
        
        Args:
            config: OrgConfig实例
            
        Raises:
            ConfigException: 参数不正确时抛出
        """
        super().check_required_params(config)
        # 这里可以添加商户进件特定的参数检查


class MerchantIncomingAddResponse(BaseResponse):
    """商户进件响应"""
    
    def __init__(self, response_dict=None):
        super().__init__(response_dict)
        self.apply_id = None  # 申请单编号
        
        if response_dict:
            self.apply_id = response_dict.get('apply_id')


@Encrypt  # 标记整个类需要加密处理
class MerchantIncomingModifyRequest(OrgBaseReq):
    """进件修改请求"""
    
    API_URL = "/open/merch/access/incoming/modify"
    
    def __init__(self):
        super().__init__()
        self.base_info = None  # 商户基础信息
        self.identification_info = None  # 商户身份信息
        self.business_info = None  # 商户经营信息
        self.settlement_info = None  # 商户结算账户信息
        self.settlement_rule = None  # 商户结算规则、费率规则
        self.additional_info = None  # 渠道信息、银行合作信息等
        self.sms_config = None  # 是否给商户发短信
        self.func_info = None  # 开通商户业务功能
        self.chain_store_info = None  # 品牌连锁商户进件配置信息
        self.brand_info = None  # 品牌信息
        
        # 定义敏感字段路径
        self._sensitive_fields = [
            # 基础信息中的敏感字段（根据ContactInfoDto.java的@Encrypt注解）
            SensitiveField("base_info.contact_info.contact_name"),
            SensitiveField("base_info.contact_info.contact_phone"),
            SensitiveField("base_info.contact_info.contact_id_no"),
            
            # 身份信息中的敏感字段（根据IdentityInfoDto.java的@Encrypt注解）
            SensitiveField("identification_info.identity_info.id_card_name"),
            SensitiveField("identification_info.identity_info.id_card_english_name"),
            SensitiveField("identification_info.identity_info.id_card_no"),
            SensitiveField("identification_info.identity_info.legal_person_phone"),
            SensitiveField("identification_info.identity_info.id_card_address"),
            
            # 结算信息中的敏感字段（根据AccountInfoDto.java的@Encrypt注解）
            SensitiveField("settlement_info.account_info.account_no"),
            SensitiveField("settlement_info.account_info.account_name"),
            SensitiveField("settlement_info.account_info.bank_set_phone"),
            SensitiveField("settlement_info.account_info.delegatee_name"),
            SensitiveField("settlement_info.account_info.delegatee_id_card"),
            SensitiveField("settlement_info.account_info.delegatee_phone"),
            
            # 补充信息中的敏感字段（根据SpecsInfoDto.java的@Encrypt注解）
            SensitiveField("additional_info.specs_info.account_no"),
            SensitiveField("additional_info.specs_info.account_name"),

            # 品牌信息中的敏感字段（根据BrandDto.java的@Encrypt注解）
            SensitiveField("brand_info.contact_name"),
            SensitiveField("brand_info.contact_phone"),
        ]
    
    def get_sensitive_fields(self):
        """
        返回需要加密的敏感字段路径列表
        
        Returns:
            包含敏感字段路径的列表
        """
        return self._sensitive_fields
    
    def set_base_info(self, base_info):
        """设置商户基础信息"""
        self.base_info = base_info
        return self
    
    def set_identification_info(self, identification_info):
        """设置商户身份信息"""
        self.identification_info = identification_info
        return self
    
    def set_business_info(self, business_info):
        """设置商户经营信息"""
        self.business_info = business_info
        return self
    
    def set_settlement_info(self, settlement_info):
        """设置商户结算账户信息"""
        self.settlement_info = settlement_info
        return self
    
    def set_settlement_rule(self, settlement_rule):
        """设置商户结算规则、费率规则"""
        self.settlement_rule = settlement_rule
        return self
    
    def set_additional_info(self, additional_info):
        """设置渠道信息、银行合作信息等"""
        self.additional_info = additional_info
        return self
    
    def set_sms_config(self, sms_config):
        """设置是否给商户发短信"""
        self.sms_config = sms_config
        return self
    
    def set_func_info(self, func_info):
        """设置开通商户业务功能"""
        self.func_info = func_info
        return self
    
    def set_chain_store_info(self, chain_store_info):
        """设置品牌连锁商户进件配置信息"""
        self.chain_store_info = chain_store_info
        return self
    
    def set_brand_info(self, brand_info):
        """设置品牌信息"""
        self.brand_info = brand_info
        return self


class MerchantIncomingModifyResponse(BaseResponse):
    """进件修改响应"""
    
    def __init__(self, response_dict=None):
        super().__init__(response_dict)
        self.apply_id = None  # 申请单编号
        
        if response_dict:
            self.apply_id = response_dict.get('apply_id')


class RealnameQueryRequest(OrgBaseReq):
    """微信支付宝认证查询请求"""
    
    API_URL = "/open/merch/access/realname/query"
    
    def __init__(self):
        super().__init__()
        self.merch_no = None  # 嘉联商户号
        self.query_type = "0"  # 0：申请单查询（默认）1：授权结果查询
        self.channel_type = "00"  # 00：所有渠道(默认) 01:wechat 02：alipay
    
    def set_merch_no(self, merch_no):
        """设置嘉联商户号"""
        self.merch_no = merch_no
        return self
    
    def set_query_type(self, query_type):
        """设置查询类型：0：申请单查询（默认）1：授权结果查询"""
        self.query_type = query_type
        return self
    
    def set_channel_type(self, channel_type):
        """设置渠道类型：00：所有渠道(默认) 01:wechat 02：alipay"""
        self.channel_type = channel_type
        return self


class RealnameQueryResponse(BaseResponse):
    """微信支付宝认证查询响应"""
    
    def __init__(self, response_dict=None):
        super().__init__(response_dict)
        self.realname_info = []  # 认证信息列表
        
        if response_dict and response_dict.get('realname_info'):
            for info in response_dict.get('realname_info', []):
                realname_info = {
                    'channel_type': info.get('channel_type'),  # 01:wechat 02：alipay
                    'applyment_status': info.get('applyment_status'),  # 申请状态
                    'qrcode_data': info.get('qrcode_data'),  # 二维码url地址
                    'reject_param': info.get('reject_param'),  # 驳回的字段名
                    'reject_reason': info.get('reject_reason'),  # 实名认证驳回原因
                    'authorize_state': info.get('authorize_state')  # 授权状态
                }
                self.realname_info.append(realname_info) 


@Encrypt  # 标记整个类需要加密处理
class MerchantOrderQueryRequest(OrgBaseReq):
    """商户申请单查询请求"""
    
    API_URL = "/open/merch/access/incoming/query"
    
    def __init__(self):
        super().__init__()
        self.apply_id = None  # 申请单号
    
    def set_apply_id(self, apply_id):
        """
        设置申请单号
        
        Args:
            apply_id: 接入方自定义的唯一编号
            
        Returns:
            MerchantOrderQueryRequest实例
        """
        self.apply_id = apply_id
        return self
    
    def check_required_params(self, config):
        """
        检查必需参数
        
        Args:
            config: OrgConfig实例
            
        Raises:
            ConfigException: 参数不正确时抛出
        """
        super().check_required_params(config)
        if not self.apply_id:
            raise ValueError("apply_id不能为空")


class MerchantOrderQueryResponse(BaseResponse):
    """商户申请单查询响应"""
    
    def __init__(self, response_dict=None):
        super().__init__(response_dict)
        self.apply_id = None  # 申请单编号
        self.audit_status = None  # 审核状态
        self.audit_detail = None  # 具体字段审核不通过原因
        self.audit_failure = None  # 总的错误描述
        self.merch_no = None  # 商户号
        self.sign_id = None  # 电子协议ID
        self.sign_status = None  # 签约状态
        self.sign_delegatee_status = None  # 他人/他人公司收款（授权收款）签约状态
        self.sign_url = None  # 电子协议签约链接
        self.sign_delegatee_url = None  # 授权收款签约链接
        self.term_no = None  # 码付加机生成的终端号
        self.audit_end_time = None  # 业务审核时间
        self.functions = None  # 业务开通情况
        
        if response_dict:
            self.apply_id = response_dict.get('apply_id')
            self.audit_status = response_dict.get('audit_status')
            self.audit_detail = response_dict.get('audit_detail')
            self.audit_failure = response_dict.get('audit_failure')
            self.merch_no = response_dict.get('merch_no')
            self.sign_id = response_dict.get('sign_id')
            self.sign_status = response_dict.get('sign_status')
            self.sign_delegatee_status = response_dict.get('sign_delegatee_status')
            self.sign_url = response_dict.get('sign_url')
            self.sign_delegatee_url = response_dict.get('sign_delegatee_url')
            self.term_no = response_dict.get('term_no')
            self.audit_end_time = response_dict.get('audit_end_time')
            self.functions = response_dict.get('functions')


@Encrypt  # 标记整个类需要加密处理
class MerchantOrderCancelRequest(OrgBaseReq):
    """商户申请单作废请求"""
    
    API_URL = "/open/merch/access/incoming/cancel"
    
    def __init__(self):
        super().__init__()
        self.apply_id = None  # 申请单号
    
    def set_apply_id(self, apply_id):
        """
        设置申请单号
        
        Args:
            apply_id: 接入方自定义的唯一编号
            
        Returns:
            MerchantOrderCancelRequest实例
        """
        self.apply_id = apply_id
        return self
    
    def check_required_params(self, config):
        """
        检查必需参数
        
        Args:
            config: OrgConfig实例
            
        Raises:
            ConfigException: 参数不正确时抛出
        """
        super().check_required_params(config)
        if not self.apply_id:
            raise ValueError("apply_id不能为空")


class MerchantOrderCancelResponse(BaseResponse):
    """商户申请单作废响应"""
    
    def __init__(self, response_dict=None):
        super().__init__(response_dict)
        # 没有其他特定字段，只使用基础响应字段ret_code和ret_msg 